
//service 数据模型，请求数据，查询数据库
const{Service}=require('egg');

class UserService extends Service{
    async getRoleCount(type){
        let sql=`select * from login where type=${type}`;
        return await this.app.mysql.query(sql);
    }
    async getPwd(username){//查询用户名
        let sql=`select * from login where username=?`;
        return await this.app.mysql.query(sql,[username]);
    };
    async upload({id,avatar_url}){//oss切片上传
        let sql=`update login set avatar_url=? where id=?`;
        return await this.app.mysql.query(sql,[avatar_url,id]);

    };
    async getUserBylogin(username){//第三方登录查询用户
        let sql=`select * from login where username=?`;
        return await this.app.mysql.query(sql,[username]);
    };
    async addUserByAccessToken({id,login,name,avatar_url}){//第三方登录添加用户
        let sql=`insert into login (id,username,avatar_url) values(?,?,?)`;
        return this.app.mysql.query(sql,[id,login,avatar_url])
    };
    async userInfo({id}){
        const sql=`select * from user where user_id=?`;
        return await this.app.mysql.query(sql,[id]);
    }
    async editUser({user_id,username,nickname,sex,tel,email}){
        const sql=`update user set username=?,nickname=?,sex=?,tel=?,email=? where user_id=?`;
        return await this.app.mysql.query(sql,[username,nickname,sex,tel,email,user_id]);//和SQL语句的值一一对应

    }
    async del({id}){//删除用户
        let sql=`delete from user where user_id=?`;
        return await this.app.mysql.query(sql,[id])
    }
    async getCount({val}){//获取总条数
        const sql=`select * from user where username like "%${val}%"`;
        const result=await this.app.mysql.query(sql);
        return result.length;
    };
    async getList({pageSize,limit,val}){//获取用户列表
        console.log(pageSize,limit,val)
        let start=(pageSize-1)*limit;//截取的起始值
        let sql =`select * from user where username like "%${val}%" limit ${start},${limit}`;
        return await this.app.mysql.query(sql)
    };
    async checkUsers({username}){//查询user用户是否存在
        let sql=`select * from user where username=?`;
        const result=await this.app.mysql.query(sql,[username]);
      
        if(result.length){//如果存在
            return false
        }else{
            return true
        };
    };
    async addusers({id,username,nickname,tel,
    email,radio}){//添加用户
        let sql=`insert into user (user_id,username,nickname,tel,email,sex) values(?,?,?,?,?,?)`;
        return await this.app.mysql.query(sql,[id,username,nickname,tel,email,radio])
    }
    async login ({username,pwd}){
        //定义SQL语句
        let sql=`select * from login where username=? and pwd=?`;
        //调用mysql的query方法
        //调用插件使用this.app调用
        return await this.app.mysql.query(sql,[username,pwd])
    };

    async checkUser(username){//查询用户
        const sql=`select * from login where username=?`;
        const result= await this.app.mysql.query(sql,[username]);
        console.log(result)
        if(result.length){//该用户已存在
            return false
        }else{
            return true
        }
    };
    async registry({id,username,pwd}){//注册
        let sql=`insert into login (id,username,pwd) values(?,?,?)`;
        return await this.app.mysql.query(sql,[id,username,pwd])
    }
}

module.exports=UserService;